<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\facade\Db;
use app\common\model\People as CommonModel;
use app\common\model\Users as UsersModel;
use app\common\validate\People as validateInfo;

/**
 * @title 首页
 * @description 接口说明
 * //@group 接口分组
 * //@header name:key require:1 default: desc:秘钥(区别设置)
 * //@param name:public type:int require:1 default:1 other: desc:公共参数(区别设置)
 */
class People extends Base
{
    private $user=[];

    function __construct(){
        parent::__construct();
        $param = input('param.');
        $token = isset($param['token'])?$param['token']:'';
        if($token) {
            $res = checkToken($param['token']);
            if ($res['code']==2) {
                return dyajaxReturn(-1, $res['msg']);
            }
            $this->user = json_decode($res['data'], true);
        }
    }

    public function list(){
        $params = input('param.');
        $Model = new CommonModel();
        // $params['userid'] = $this->user['user_id'];
        $params['house_id'] = $params['house_id'];
        $res = $Model->tableData($params);
        return dyajaxReturn(1,'获取成功',$res['data'],'',$res['count']);
    }

    public function add(){
        $param = input("param.");

        $validate = new validateInfo;
        $result = $validate->scene('add')->batch(false)->check($param);
        if(!$result){
            return dyajaxReturn(0,$validate->getError());
        }

        $Model = new CommonModel();
        $param['act'] = "add";
        $param['userid'] = $this->user['user_id'];
        $param['picurl'] = implode(",",$param['picurl']);
        $res = $list = $Model ->handle($param);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }


    public function edit(){
        $param = input("param.");

        $validate = new validateInfo;
        $result = $validate->scene('edit')->batch(false)->check($param);
        if(!$result){
            return dyajaxReturn(0,$validate->getError());
        }

        if(!isset($param['imageList']) && count($param['imageList']) < 1 ){
            return dyajaxReturn(0,"图片不能为空");
        }

        $Model = new CommonModel();
        $param['act'] = "edit";
        $param['userid'] = $this->user['user_id'];
        $param['picurl'] = implode(",",$param['imageList']);
        unset($param['imageList']);
        $res = $list = $Model ->handle($param);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }


    public function getInfo(){
        $param = input("param.");

        $Model = new CommonModel();
        $res = $Model->info($param['id']);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }

    public function delete(){
        $param = input("param.");
        $Model = new CommonModel();
        $res = $Model->deleted($param['id']);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }

    //生成用户
    public function addUsers(){
        $param = input("param.");
        $Model = new CommonModel();
        $people_id = $param['people_id'];
        $people_one = $Model->where("id","=",$people_id)->find();

        $UsersModel = new UsersModel();
        $user_one = $UsersModel->where([
            "type"=>2,
            "mobile"=>$people_one['mobile'],
            "people_id"=>$people_id
        ])->find();
        if(!$user_one){
            //没有用户，生成用户
            $data['act'] = 'add';
            $data['type'] = 2;
            $data['name'] = $people_one['name'];
            $data['mobile'] = $people_one['mobile'];
            $data['people_id'] = $people_one['id'];

            $data['sex'] = $people_one['sex'];
            $data['card_num'] = $people_one['card_num'];
            $data['card_address'] = $people_one['card_address'];
            $data['unit_name'] = $people_one['unit_name'];
            $data['unit_position'] = $people_one['unit_position'];
            $data['picurl'] = $people_one['picurl'];

            $user_res = $UsersModel->handle($data);
            if(!$user_res['code']){
                return dyajaxReturn(0,$user_res['msg']);
            }
            $user_id = $user_res['data'];
            $vovo['true_user_id'] = $user_id;
            $m_res = $Model->where("id","=",$people_id)->update($vovo);
            if(!$m_res){
                return dyajaxReturn(0,"生产失败");
            }
            return dyajaxReturn(1,"生产用户成功");
        }else{
            return dyajaxReturn(0,"用户已存在");
        }

    }

}
